package mypassForStu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mypassForStu.common.R;
import mypassForStu.entity.Department;
import mypassForStu.entity.Notice;
import mypassForStu.entity.Student;
import mypassForStu.entity.Teacher;
import mypassForStu.service.DepartmentService;
import mypassForStu.service.NoticeService;
import mypassForStu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private NoticeService noticeService;

    //注册
    @PostMapping("/register")
    public R<Student> register(HttpServletRequest request, @RequestBody Student student){
        //根据id查询
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentId, student.getStudentId());
        Student student1 = studentService.getOne(queryWrapper);
        if(null!=student1){
            return R.error("id已存在！");
        }
        studentService.save(student);
        request.getSession().setAttribute("student", student.getStudentId());
        return R.success(student);
    }

    //登录
    @PostMapping("/login")
    public R<Student> login(HttpServletRequest request, @RequestBody Student student){
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentId, student.getStudentId());
        Student stu = studentService.getOne(queryWrapper);

        if(stu!=null){
            if(stu.getPassword().equals(student.getPassword())){
                request.getSession().setAttribute("student", stu.getStudentId());
                return R.success(stu);
            }
            return R.error("密码不正确");
        }
        return R.error("用户不存在");
    }

    //修改个人信息
    @PutMapping("/update")
    public R<Student> update(HttpServletRequest request, @RequestBody Student student){

        //修正数字类型被更新的错误
        Student student1 = studentService.getById(student.getStudentId());
        student.setGrade(student1.getGrade());
        student.setDepartId(student1.getDepartId());

        studentService.updateById(student);
        request.getSession().setAttribute("student", student.getStudentId());
        student = studentService.getById(student.getStudentId());
        return R.success(student);
    }

    //获取院系信息（用于注册）
    @PostMapping("/getDeparts")
    public R<List<Department>> getDeparts(){

        List<Department> departs = departmentService.list();
        if (departs.size()<1){
            return R.error("没有学院！");
        }
        return R.success(departs);
    }

    //获取所有学院通知
    @PostMapping("/getNotices")
    public R<List<Notice>> getNotices(HttpServletRequest request){

        //从前端获取学生id
        long id = (long) request.getSession().getAttribute("student");
        Student student = studentService.getById(id);
        if(null==student){
            return R.error("请重新登录");
        }
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getDepartId, student.getDepartId());
        List<Notice> notices = noticeService.list(queryWrapper);

        if(notices.size() < 1){
            return R.error("无通知");
        }

        return R.success(notices);
    }

    //查看某条通知
    @PostMapping("/getNotice")
    public R<Notice> getNotice(@RequestBody Notice notice){
        Notice notice1 = noticeService.getById(notice.getId());
        return R.success(notice1);
    }
}
